-----------------------------------------------------------------------------------------------------------------------------------------
--解析globalTableTokenTable生成要执行的命令到globalTableCommandTable
-----------------------------------------------------------------------------------------------------------------------------------------
--print("Command Parse lua")
local localStrLuaName="CommandParse.lua"
-----------------------------------------------------------------------------------------------------------------------------------------
--全局变量存储全局信息
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
--common tools 
-----------------------------------------------------------------------------------------------------------------------------------------
if nil == globalScriptEmbled2C then
	require(globalStrModuleUtilities)
end
-----------------------------------------------------------------------------------------------------------------------------------------
--生成命令函数群
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
--generateCFlagsOptions 生成
-----------------------------------------------------------------------------------------------------------------------------------------
function generateCFlagsOptions (target,source)
--target[gTableDefinitions]
	local definitions = target[gTableDefinitions]
	if nil == definitions then
		return 
	end
	local definitionsCflagsStr = ""
	for i,v in pairs(definitions) do 
		if "table" == type(v) then
			for j,x in pairs(v) do
				if "string" == type(x) then
					definitionsCflagsStr = definitionsCflagsStr.." "..x
				end
			end
		end
		if "string" == type(v) then
			definitionsCflagsStr = definitionsCflagsStr.." "..v
		end
	end
	if "" ~= definitionsCflagsStr then
		table.insert(target[gTableCFLAGS],definitionsCflagsStr)
	end
--target[gTableIncludeDirectories]
	local include = target[gTableIncludeDirectories]
	if nil == include then
		return 
	end
	local includeCflagsStr = ""
	for i,v in pairs(include) do 
		if "table" == type(v) then
			for j,x in pairs(v) do
				if "string" == type(x) then
					includeCflagsStr = includeCflagsStr.." -I"..source.."/"..x
				end
			end
		end
		if "string" == type(v) then
			includeCflagsStr = includeCflagsStr.." -I"..source.."/"..v
		end
	end
	if "" ~= includeCflagsStr then
		table.insert(target[gTableCFLAGS],includeCflagsStr)
	end
--target[gTableRequiredPackage]
	local requiredpackage = target[gTableRequiredPackage]
	if nil == requiredpackage then
		return 
	end
	local requiredpackageCflagsStr = ""
	for i,v in pairs(requiredpackage) do 
		if "table" == type(v) then
			for j,x in pairs(v) do
				if "string" == type(x) then
					local index = string.lower(x).."_cflags"
					local tmpCflags = globalTableSystemCompilerOption[index]
					if nil ~= tmpCflags then
						requiredpackageCflagsStr = requiredpackageCflagsStr..tmpCflags
					end
				end
			end
		end
		if "string" == type(v) then
			local index = string.lower(v).."_cflags"
			local tmpCflags = globalTableSystemCompilerOption[index]
			if nil ~= tmpCflags then
				requiredpackageCflagsStr = requiredpackageCflagsStr..tmpCflags
			end
		end
	end
	if "" ~=requiredpackageCflagsStr then
		table.insert(target[gTableCFLAGS],requiredpackageCflagsStr)
	end
--default cflags
	table.insert(target[gTableCFLAGS],globalTableSystemCompilerOption["cflags"])

	local cflagsString = table.concat (target[gTableCFLAGS]," ")
	target[gTableCFLAGS] = cflagsString
end
-----------------------------------------------------------------------------------------------------------------------------------------
--copyParentOption2Child
-----------------------------------------------------------------------------------------------------------------------------------------
function copyParentOption2Child(target)
	local children = target[gTableSubDirectory]
	for i,v in pairs(children) do
		table.insert(v[gTableCFLAGS],target[gTableCFLAGS])
	end
end
-----------------------------------------------------------------------------------------------------------------------------------------
--generateSrc2ByteCodeCommand 生成
-----------------------------------------------------------------------------------------------------------------------------------------
function generateSrc2ByteCodeCommand(target,targetSrc,currentDir,sourceDir)
	local ret = {}
	if 0 == table.maxn(targetSrc) then
		print("Warning:empty source")
	end
	for i,v in pairs(targetSrc) do
		local CCCommandString = sourceDir.."/"..v
		local TokenTable = {}
		TokenTable = nativeToolString2Token(v,"./\\")
		local tokenTableSize = table.maxn(TokenTable)
		if 2 > tokenTableSize then
			print("Warning:unknown file ".."%"..v.."%")
			return ret
		end
		local suffix = TokenTable[tokenTableSize]
		local BaseName = TokenTable[tokenTableSize-1]
--		print(BaseName,suffix)
		local bcName = currentDir.."/ByteCode/"..BaseName..".bc"
		CCCommandString = CCCommandString.." -o " ..bcName.." "..target[gTableCFLAGS]
--.c
		if "c" == suffix then
			CCCommandString = "${CC} "..CCCommandString
			table.insert(ret,bcName)
		elseif "cpp" == suffix or "cc" == suffix or "cp" == suffix or "cx" == suffix or "cxx" == suffix then
--.cpp .cc .cp .cx .cxx
			CCCommandString = "${CXX} "..CCCommandString
			table.insert(ret,bcName)
		elseif "asm" == suffix then 
--.asm .as
			CCCommandString = "${AS} "..CCCommandString
--header file
		elseif "h" == suffix then
--Warning Unknown file type
		else
			print("Warning Unknown file type "..v)
		end
		table.insert(gTableExcuteCommand,CCCommandString)
	end
	return ret
end
-----------------------------------------------------------------------------------------------------------------------------------------
--LinkOptionByteCodeCommandGen根据代码文件名字生成编译命令
-----------------------------------------------------------------------------------------------------------------------------------------
function LinkOptionByteCodeCommandGen(target,targetTable)
	local tmpLibOption= {}
	local libNameTable = targetTable[gTableTargetLink]
	if nil == libNameTable then
		return nil
	end
	for i,v in pairs(libNameTable) do
--自己定义的Lib库直接连接ByteCode
		local selfLibPath = globalTableTargetPathTable[v];
		if nil ~= selfLibPath then
			table.insert(tmpLibOption,selfLibPath)
--系统或者第三方的Lib库
		else
--			local libName = skipHeadSpace(v)
--			if nil ~= libName then
--				table.insert(tmpLibOption,string.format('%s%s',"-l",v))
--			end
		end
	end
	local ret = table.concat(tmpLibOption," ")
	return ret 
end
-----------------------------------------------------------------------------------------------------------------------------------------
--generatelinkByteCodeCommand 生成
-----------------------------------------------------------------------------------------------------------------------------------------
function generatelinkByteCodeCommand(target,targetTable,current,targetNameByteCode)
	local tmpTargetCommand = {}
	local libString = ""
	local typeTable = targetTable[gTableTargetType][target]
	local tagetLocal = current.."/ByteCode/"
	if "EXEC" == typeTable[1] then
		tagetLocal = tagetLocal.."/"..target 
		tmpTargetCommand = {"${LD_EXE}","-o",tagetLocal,targetNameByteCode}
		table.insert(tmpTargetCommand,targetTable[gTableLDFLAGS][target])
		--Lib -l 追加
		libString = LinkOptionByteCodeCommandGen(target,targetTable)
		if nil ~= libString then
			table.insert(tmpTargetCommand,libString)
		end
	elseif "SHARED" == typeTable[1] then
		tagetLocal = tagetLocal.."lib"..target..".bc" 
		tmpTargetCommand = {"${LD_LIB}","-o",tagetLocal,targetNameByteCode}
		--Lib -l 追加
		libString = LinkOptionByteCodeCommandGen(target,targetTable)
		if nil ~= libString then
			table.insert(tmpTargetCommand,libString)
		end
	elseif "STATIC" == typeTable[1] then
		tagetLocal = tagetLocal.."lib"..target..".bc" 
		tmpTargetCommand = {"${LD_LIB}"," -o ",tagetLocal,targetNameByteCode}
	elseif "MODULE" == typeTable[1] then
		print("now ignore MODULE")
	else
		print("impossible")
	end
--命令追加
	if 0 < table.maxn(tmpTargetCommand) then
		local tmpTargetCommandStr = table.concat(tmpTargetCommand," ")
		table.insert(gTableExcuteCommand,tmpTargetCommandStr)
	end
-- add target path to global 
	globalTableTargetPathTable[target] = tagetLocal;
	local targetDepen = target..";type="..typeTable[1]
	
	local libPath = table.concat(targetTable[gTableLinkDirectories]," ")
	if nil == libPath then
		libPath = " "
	end
	targetDepen = targetDepen..";path="..libPath
	
	local libTable = targetTable[gTableTargetLink]
	local libName = ""
	if "table" == type(libTable) then
		libName = table.concat(libTable," ")
	elseif "string" == type(libTable) then
		libName = libTable
	end
	if nil == libName then
		libName = " "
	end
	targetDepen = targetDepen..";lib="..libName
	table.insert(globalTableTargetDependenceTable,targetDepen)
end
-----------------------------------------------------------------------------------------------------------------------------------------
--generateCCCXXCommand 生成
-----------------------------------------------------------------------------------------------------------------------------------------
function generateCCCXXCommand(target,current,source)
--	target[gTableTargetName]
	local ccTarget = target[gTableTargetName]
	if nil == ccTarget then
		return 
	end
	for i,v in pairs(ccTarget) do
		local targetSrc = target[gTableTargetSrc][v]
		if nil == targetSrc then
			return 
		end
		local objNameTmp = generateSrc2ByteCodeCommand(target,targetSrc,current,source)
--生成的BC文件，合并成一个文件
		local objNameTmpStr = table.concat(objNameTmp," " )
		local targetNameByteCode = current.."/ByteCode/".."LLMakeAutoGenName_"..v..".bc"
		local tmpTargetCommandStr = "${BC_LINK}".." -o "..targetNameByteCode.." "..objNameTmpStr
		table.insert(gTableExcuteCommand,tmpTargetCommandStr)
--链接ByteCode命令
--		linkByteCodeCommandGen(v,localTableTargetTypeTable[v],targetNameByteCode)
		generatelinkByteCodeCommand(v,target,current,targetNameByteCode)
--目标生成链接命令
	end
end
-----------------------------------------------------------------------------------------------------------------------------------------
--doAllCommand
-----------------------------------------------------------------------------------------------------------------------------------------
function doAllCommand(target,current,source)
--ByteCode文件夹生成
	local tmpTargetCommandStr = "${MKDIR} "..current.."/ByteCode"
	table.insert(gTableExcuteCommand,tmpTargetCommandStr)
--cflags生成
	generateCFlagsOptions(target,source)

-- copy parent to child option.
	copyParentOption2Child(target)
	
--cc(cxx) comand生成
	generateCCCXXCommand(target,current,source)
	
--	debug_dump_Table("target",target)
-- do sub command
	local sub =  target[gTableSubDirectory]
	if sub then
		for i,v in pairs(sub) do
			doAllCommand(v,current.."/"..i,source.."/"..i)
		end
	end
end
-----------------------------------------------------------------------------------------------------------------------------------------------
-- 开始 
-----------------------------------------------------------------------------------------------------------------------------------------------
function doAllCommandFile()
--	debug_dump_Table(gTableParseTargetData)
	doAllCommand(gTableParseTargetData,globalStrRootDir,globalStrSourceRoot)
end

if nil == globalScriptEmbled2C then
	doAllCommandFile()
---
	for i,v in pairs(gTableExcuteCommand) do 
		print(i,v)
	end
end
